<template>
  <div>
    <el-row>
      <el-col :span="24" style="margin:5px 0">
        <el-button @click="previousPage" style="float:right; " type="success" plain icon="el-icon-arrow-left">
          返回</el-button>
      </el-col>
    </el-row>
    <el-form ref="form" :model="infoForm" label-width="150px" style="width:100%">
      <el-form-item label="策略名称：">
        <el-input v-model="infoForm.name"></el-input>
      </el-form-item>
      <div v-if="formType!=='runRes'">
        <el-form-item :label="formlabel">
          <el-button type="text" icon="el-icon-plus" @click="addtableClick">新增</el-button>
          <el-table :data="tableData" border height="200">
            <el-table-column prop="name" :label="tablelabel" align="center"></el-table-column>
            <el-table-column label="操作" width="120" align="center">
              <template slot-scope="scope">
                <el-button @click.native.prevent="deleteRow(scope.$index, tableData)" type="text" size="small">
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </div>
      <div v-if="formType=='runRes'">
        <el-row>
          <el-col :span="24">
            <el-form-item label="CPU监控：">
              当CPU使用率超过<el-input v-model="infoForm.CPU1" style="width:80px"></el-input>%并且持续时间超过 <el-input
                v-model="infoForm.CPU2" style="width:50px"></el-input>秒时报警
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内存监控：">
              当内存使用率超过<el-input v-model="infoForm.Memory1" style="width:50px"></el-input>%并且持续时间超过 <el-input
                v-model="infoForm.Memory2" style="width:50px"></el-input>秒时报警
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="硬盘监控：">
              当磁盘分区剩余空间低于<el-input v-model="infoForm.Disk" style="width:80px"></el-input>M时候报警
            </el-form-item>
          </el-col>
        </el-row>

      </div>
      <el-form-item>
        <el-button type="primary" @click="loginSubmit">立即创建</el-button>
        <el-button @click="cancelHandle">取消</el-button>
      </el-form-item>
    </el-form>

    <el-dialog :title="dialogtitle" :visible.sync="dialogVisible" width="40%">
      <el-row>
        <el-form label-position="right" label-width="100px" :model="tableform">
          <el-col :span="20">
            <el-form-item label="名称">
              <el-input size="mini" v-model="tableform.name"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onSubmit" size="mini">确 认</el-button>
        <el-button size="mini" @click="dialogVisible=false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userName: '',
      infoForm: {
        name: '',
        CPU1:'',
        CPU2:'',
        Memory1:'',
        Memory2:{},
        Disk:{
            System:{}
        },
      },
      formType: '',
      tableData: [],
      dialogVisible: false,
      dialogtitle: '',
      tableform: {},
      formlabel: "",
      tablelabel: "",
      form: {
        name: '',
        creator: sessionStorage.getItem('userName'),
        enableTime: '2022-09-05T17:24:00',
        state: '0',
        Root: {
          Policy: {
            type: '',
            disable: '0',
            name: '',
          }
        }
      },

    }
  },
  created() {
    this.gettypeName(this.$route.params.type, this.$route.params.row)
    this.userName = sessionStorage.getItem('userName');

    if (this.$route.params.row) {
      this.form = this.$route.params.row
      console.log(this.$route.params.row,124);

      this.infoForm.name = this.form.name;
      if (this.formType == 'runRes') {
        this.infoForm.CPU1 = this.form.Root.Policy.CPU.rate
        this.infoForm.CPU2 = this.form.Root.Policy.CPU.time
        this.infoForm.Memory1 = this.form.Root.Policy.Memory.rate
        this.infoForm.Memory2 = this.form.Root.Policy.Memory.time
        this.infoForm.Disk = this.form.Root.Policy.Disk.System.space;

        return;
      }
      console.log(this.form.Root.Policy.Monitor,'monitor');
      let monitorString =  this.form.Root.Policy.Monitor;
      if(monitorString!==''){
          this.tableData =[];
          let Monits = []
          Monits = monitorString.split(',')
          for (let i = 0; i < Monits.length; i++) {
            this.tableData.push({name:Monits[i]})
        }
      }else{
        this.tableData =[]
      }
    }
  },
  methods: {
    //返回上一页
    previousPage() {
      this.$router.go(-1)
    },
    gettypeName(type, row) {
      if (type == "processBehavior" || type == "processStatus") {   // 进程行为 进程状态
        this.formType = type
        this.formlabel = "进程检测规则"
        this.tablelabel = "进程名称"
      } else if (type == "system") {  // 
        this.formType = type
        this.formlabel = "服务监控规则"
        this.tablelabel = "服务名称"
      } else if (type == "runRes") { // 
        this.formType = type
      }
    },
    loginSubmit() {
      var url = ''
      this.form.name = this.infoForm.name;
      if (this.formType == 'runRes') {
        url = '/sjrz/policy/performance'
        let cpu = {
          rate: this.infoForm.CPU1,
          time: this.infoForm.CPU2,
        }
        let Memory = {
          rate: this.infoForm.Memory1,
          time: this.infoForm.Memory2,
        }
        let disk = {
                space: this.infoForm.Disk,
                time:'60'
        }

        this.form.Root={
                Policy:{
                    CPU:cpu,
                    Memory:Memory,
                    Disk:{
                        System:disk,
                    },
                    type:'performance',
                    name:this.infoForm.name,
                    disable:'0'
                }
            }

        // this.form.Root.Policy.CPU = cpu;
        // this.form.Root.Policy.Memory = Memory;
        // this.form.Root.Policy.Disk.System = disk;
        // this.form.Root.Policy.type = "performance";
        // this.form.Root.Policy.name = this.infoForm.name;
      }
      if (this.formType == 'system') {
        url = '/sjrz/policy/service'
        this.form.Root.Policy.type = "service";
        this.form.Root.Policy.name = this.infoForm.name;
        let Monis = []
        this.tableData.forEach(item => {
          console.log(item,176 );
          Monis.push(item.name)
        })
        let Monitor = Monis.join(',')
        this.form.Root.Policy.Monitor = Monitor
        console.log(this.tableData,185);
      }
      if (this.formType == 'processStatus') {
        url = '/sjrz/policy/process'
        this.form.Root.Policy.type = "process";
        this.form.Root.Policy.name = this.infoForm.name;
        let Monis = []
        this.tableData.forEach(item => {
          console.log(item,176 );
          Monis.push(item.name)
        })
        let Monitor = Monis.join(',')
        this.form.Root.Policy.Monitor = Monitor
      }
      console.log(this.form, 789);
      this.$postForJson(url, {policy:this.form}).then(res => {
        let { data } = res
        if (data.errorcode == '0') {
          this.$message({
            type: "success",
            message: "提交成功！",
          });
          this.form = {}
          this.$router.go(-1)
        }
      })
    },
    //取消按钮
    cancelHandle() {
      this.$router.go(-1);
    },
    //删除按钮
    deleteRow(index, rows) {
      rows.splice(index, 1);
    },
    //新增按钮
    addtableClick() {
      this.dialogtitle = "新增"
      this.dialogVisible = true
    },
    //页面提交
    onSubmit() {
      this.dialogVisible = false
      this.tableData.push(this.tableform)
      this.tableform = {}
    },
  }
}
</script>

<style scoped>
</style>